const API = require('../../utils/api')
const pathUrl = require('../../utils/common.js')
Page({
  data: {
    court: {},
    reviews: []
  },

  onLoad(options) {
    const id = options.id
    if (id) {
      API.nearByPlaceById(id).then(res=>{
        res.image = pathUrl + res.image
        this.setData({
          court:res
        })
        wx.setNavigationBarTitle({
          title: res.name
        })
      })
      //TODO 用户评价
      //this.loadReviews(id)
    }
  },


  async loadReviews(id) {
    try {
      const res = await getApp().request({
        url: `/courts/${id}/reviews`,
        data: { limit: 3 }
      })

      if (res.data) {
        this.setData({
          reviews: res.data.reviews || []
        })
      }
    } catch (error) {
      console.error('加载评价失败:', error)
    }
  },

  openMap() {
    const { court } = this.data
    wx.openLocation({
      latitude: court.latitude,
      longitude: court.longitude,
      name: court.name,
      address: court.address
    })
  },

  contactCourt() {
    wx.makePhoneCall({
      phoneNumber: this.data.court.phoneNo
    })
  },

  bookCourt() {
    const { court } = this.data
    wx.navigateTo({
      url: `/pages/booking/booking?courtId=${court.id}&courtName=${court.name}`
    })
  },

  goToReviews() {
    const { court } = this.data
    wx.navigateTo({
      url: `/pages/court-reviews/court-reviews?courtId=${court.id}`
    })
  },

  onShareAppMessage() {
    const { court } = this.data
    return {
      title: `${court.name} - ${court.price}/小时`,
      path: `/pages/court-detail/court-detail?id=${court.id}`,
      imageUrl: court.images[0]
    }
  }
})